package com.fengba.nanzhi.setup;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.fengba.nanzhi.fengba.R;
import com.fengba.nanzhi.utils.OKManager;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by duan on 2018/4/2.
 */

public class ForGet02 extends AppCompatActivity {


    //手机号
    private EditText FGphone;
    //密码
    private EditText FGpassword;
    //眼睛图片 ImageButton
    private ImageButton FGeye;
    //验证码
    private Button YZMButton;

    private EditText FGcode;

    private TimeCount time;
    //msg_id
    private String msg_id="0";

    //新建
    private Button FGindex;

    //XX
    private ImageButton FGclose;

    //返回
    private ImageButton FGretun;

    //菊花
    private ProgressBar FGjuhua;

    public ForGet02() {
    }


    @SuppressLint("WrongViewCast")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fx_forget02);
        FGphone = (EditText) findViewById(R.id.FGphone);
        FGphone.setInputType(EditorInfo.TYPE_CLASS_PHONE);
        FGpassword = (EditText) findViewById(R.id.FGpassword);
        time = new TimeCount(60000, 1000);
        FGcode = (EditText) findViewById(R.id.FGcode);
        FGcode.setInputType(EditorInfo.TYPE_CLASS_PHONE);
        FGjuhua = (ProgressBar) findViewById(R.id.FGjuhua);


        /**
         * 更换眼睛
         */
        FGeye = (ImageButton) findViewById(R.id.FGeye);
        FGeye.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int type = FGpassword.getInputType();
                if (type == 109) {
                    FGpassword.setInputType(129);
                    ((ImageButton) view).setImageDrawable(getResources().getDrawable(R.mipmap.img_0003));
                } else {
                    FGpassword.setInputType(109);
                    ((ImageButton) view).setImageDrawable(getResources().getDrawable(R.mipmap.img_0002));
                }
            }
        });
        /**
         * 叉叉
         */
        FGclose = (ImageButton) findViewById(R.id.FGclose);
        FGclose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FGpassword.setText("");
                FGclose.setVisibility(View.INVISIBLE);
            }

        });
        /**
         * 显示xx
         */
        FGpassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                ////Log.i(TAG, "输入文字中的状态，count是输入字符数");

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {

                ////Log.i(TAG, "输入文本之前的状态");
            }

            @Override
            public void afterTextChanged(Editable s) {
                ////Log.i(TAG, "输入文字后的状态");
                FGclose.setVisibility(View.VISIBLE);
            }
        });
        /**
         * 修改密码POST
         */
        FGindex = (Button) findViewById(R.id.FGindex);
        FGindex.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FGjuhua.setVisibility(ProgressBar.VISIBLE);
                if (FGphone.length() == 11) {
                    OKManager.getInstance().type2014(String.valueOf(FGphone.getText()),
                            String.valueOf(FGpassword.getText()),
                            msg_id,
                            String.valueOf(FGcode.getText())
                            ,new OKManager.func4() {
                                @Override
                                public void onResponse(JSONObject jsonObject) {
                                    try {
                                        if (jsonObject.get("code").equals(0)) {
                                            finish();
                                            Toast.makeText(ForGet02.this, jsonObject.get("message").toString(), Toast.LENGTH_SHORT).show();
                                            FGjuhua.setVisibility(ProgressBar.INVISIBLE);
                                        } else {
                                            Toast.makeText(ForGet02.this, jsonObject.get("message").toString(), Toast.LENGTH_SHORT).show();
                                            FGjuhua.setVisibility(ProgressBar.INVISIBLE);
                                        }
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }
                                }

                                @Override
                                public void onFailure(String string) {
                                    Toast.makeText(ForGet02.this, "网络错误", Toast.LENGTH_SHORT).show();
                                    FGjuhua.setVisibility(ProgressBar.INVISIBLE);
                                }
                            });

                } else {
                    FGjuhua.setVisibility(ProgressBar.INVISIBLE);
                    Toast.makeText(ForGet02.this, "请输入正确手机号", Toast.LENGTH_SHORT).show();
                }
            }

        });

        /**
         * 发送验证码
         */
        YZMButton = (Button) findViewById(R.id.YZMButton);
        FGcode.setInputType(EditorInfo.TYPE_CLASS_PHONE);
        YZMButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                if (FGphone.length() == 11) {
                    time.start();
                    OKManager.getInstance().type2015(
                        "1",
                        String.valueOf(FGphone.getText())
                        ,new OKManager.func4() {
                            @Override
                            public void onResponse(JSONObject jsonObject) {
                                try {
                                    if (jsonObject.get("code").equals(0)) {
                                        JSONObject data = (JSONObject) jsonObject.get("data");
                                        msg_id = data.get("message").toString();

                                    } else {
                                        Toast.makeText(ForGet02.this, jsonObject.get("message").toString(), Toast.LENGTH_SHORT).show();
                                        FGjuhua.setVisibility(ProgressBar.INVISIBLE);
                                    }
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }

                            @Override
                            public void onFailure(String string) {
                                Toast.makeText(ForGet02.this, "网络错误", Toast.LENGTH_SHORT).show();
                                FGjuhua.setVisibility(ProgressBar.INVISIBLE);
                            }
                        });




                } else {
                    Toast.makeText(ForGet02.this, "请输入正确手机号", Toast.LENGTH_SHORT).show();
                }
            }


        });
        /**
         * 返回
         */
        FGretun = (ImageButton) findViewById(R.id.FGretun);
        FGretun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }//都在这里打

    //计时器实现
    class TimeCount extends CountDownTimer {

        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            YZMButton.setClickable(false);
            YZMButton.setText("(" + millisUntilFinished / 1000 + ") ");
        }

        @Override
        public void onFinish() {
            YZMButton.setText("重新获取验证码");
            YZMButton.setClickable(true);
        }
    }

    /**
     * 隐藏软键盘
     *
     * @param ev
     * @return
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if (isShouldHideInput(v, ev)) {

                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                }
            }
            return super.dispatchTouchEvent(ev);
        }
        // 必不可少，否则所有的组件都不会有TouchEvent了
        if (getWindow().superDispatchTouchEvent(ev)) {
            return true;
        }
        return onTouchEvent(ev);
    }

    public boolean isShouldHideInput(View v, MotionEvent event) {
        if (v != null && (v instanceof EditText)) {
            int[] leftTop = {0, 0};
            //获取输入框当前的location位置
            v.getLocationInWindow(leftTop);
            int left = leftTop[0];
            int top = leftTop[1];
            int bottom = top + v.getHeight();
            int right = left + v.getWidth();
            if (event.getX() > left && event.getX() < right
                    && event.getY() > top && event.getY() < bottom) {
                // 点击的是输入框区域，保留点击EditText的事件
                return false;
            } else {
                return true;
            }
        }
        return false;
    }

}
